﻿using System;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using PointOfSale.Global;
using PointOfSale.Class;
using PointOfSale.Configuration.Vendor;
using PointOfSale.Configuration.Item;

namespace PointOfSale.Transaction
{
	public class frmEditPurchaseBill : System.Windows.Forms.Form
	{
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.DateTimePicker dateTimePicker1;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.LookupComboBox cboItemName;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.TextBox txtQty;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.LookupComboBox cboUnit;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.TextBox txtRate;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.TextBox txtTax;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.TextBox txtAmount;
		private System.Windows.Forms.Label label11;
		private System.Windows.Forms.TextBox txtTotalAmount;
		private System.Windows.Forms.Label label12;
		private System.Windows.Forms.TextBox txtTotalTax;
		private System.Windows.Forms.Label label13;
		private System.Windows.Forms.TextBox txtDisPerCent;
		private System.Windows.Forms.Label label14;
		private System.Windows.Forms.TextBox txtDisAmount;
		private System.Windows.Forms.Label label15;
		private System.Windows.Forms.TextBox txtBillAmount;
		private System.Windows.Forms.Button btnDelete;
		private System.Windows.Forms.Button btnUpdate;
		private System.Windows.Forms.Button btnAdd;
		private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.Button btnClose;
		private System.Windows.Forms.Button btnVendorSelect;
		private System.Windows.Forms.ListView lsvView;
		private System.Windows.Forms.Button btnItemAdd;
		private System.Windows.Forms.ColumnHeader colItemName;
		private System.Windows.Forms.ColumnHeader colItemId;
		private System.Windows.Forms.ColumnHeader colQty;
		private System.Windows.Forms.ColumnHeader colUnitName;
		private System.Windows.Forms.ColumnHeader colUnitId;
		private System.Windows.Forms.ColumnHeader colRate;
		private System.Windows.Forms.ColumnHeader colAmount;
		public System.Windows.Forms.LookupComboBox cboVendor;

		DataTable danhSachMatHang = null;

		private int purchaseId;

		public frmEditPurchaseBill(int pi)
		{
			this.purchaseId = pi;
			InitializeComponent();
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.cboVendor = new System.Windows.Forms.LookupComboBox();
			this.btnVendorSelect = new System.Windows.Forms.Button();
			this.label4 = new System.Windows.Forms.Label();
			this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
			this.label2 = new System.Windows.Forms.Label();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.btnItemAdd = new System.Windows.Forms.Button();
			this.lsvView = new System.Windows.Forms.ListView();
			this.colItemName = new System.Windows.Forms.ColumnHeader();
			this.colItemId = new System.Windows.Forms.ColumnHeader();
			this.colQty = new System.Windows.Forms.ColumnHeader();
			this.colUnitName = new System.Windows.Forms.ColumnHeader();
			this.colUnitId = new System.Windows.Forms.ColumnHeader();
			this.colRate = new System.Windows.Forms.ColumnHeader();
			this.colAmount = new System.Windows.Forms.ColumnHeader();
			this.btnDelete = new System.Windows.Forms.Button();
			this.btnUpdate = new System.Windows.Forms.Button();
			this.btnAdd = new System.Windows.Forms.Button();
			this.txtAmount = new System.Windows.Forms.TextBox();
			this.label10 = new System.Windows.Forms.Label();
			this.txtRate = new System.Windows.Forms.TextBox();
			this.label8 = new System.Windows.Forms.Label();
			this.cboUnit = new System.Windows.Forms.LookupComboBox();
			this.label7 = new System.Windows.Forms.Label();
			this.txtQty = new System.Windows.Forms.TextBox();
			this.label6 = new System.Windows.Forms.Label();
			this.cboItemName = new System.Windows.Forms.LookupComboBox();
			this.label5 = new System.Windows.Forms.Label();
			this.txtTax = new System.Windows.Forms.TextBox();
			this.label9 = new System.Windows.Forms.Label();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.txtBillAmount = new System.Windows.Forms.TextBox();
			this.label15 = new System.Windows.Forms.Label();
			this.txtDisAmount = new System.Windows.Forms.TextBox();
			this.label14 = new System.Windows.Forms.Label();
			this.txtDisPerCent = new System.Windows.Forms.TextBox();
			this.label13 = new System.Windows.Forms.Label();
			this.txtTotalTax = new System.Windows.Forms.TextBox();
			this.label12 = new System.Windows.Forms.Label();
			this.txtTotalAmount = new System.Windows.Forms.TextBox();
			this.label11 = new System.Windows.Forms.Label();
			this.btnSave = new System.Windows.Forms.Button();
			this.btnClose = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.cboVendor);
			this.groupBox1.Controls.Add(this.btnVendorSelect);
			this.groupBox1.Controls.Add(this.label4);
			this.groupBox1.Controls.Add(this.dateTimePicker1);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox1.Location = new System.Drawing.Point(8, 0);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(640, 64);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Thông tin mua";
			// 
			// cboVendor
			// 
			this.cboVendor.AllowTypeAllSymbols = true;
			this.cboVendor.BackColor = System.Drawing.SystemColors.ControlLightLight;
			this.cboVendor.Location = new System.Drawing.Point(160, 32);
			this.cboVendor.Name = "cboVendor";
			this.cboVendor.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.cboVendor.Size = new System.Drawing.Size(120, 21);
			this.cboVendor.TabIndex = 10;
			this.cboVendor.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cboVendor_KeyDown);
			this.cboVendor.SelectedIndexChanged += new System.EventHandler(this.cboVendor_SelectedIndexChanged);
			// 
			// btnVendorSelect
			// 
			this.btnVendorSelect.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnVendorSelect.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnVendorSelect.Location = new System.Drawing.Point(288, 32);
			this.btnVendorSelect.Name = "btnVendorSelect";
			this.btnVendorSelect.Size = new System.Drawing.Size(32, 23);
			this.btnVendorSelect.TabIndex = 8;
			this.btnVendorSelect.Text = "...";
			this.btnVendorSelect.Click += new System.EventHandler(this.btnVendorSelect_Click);
			// 
			// label4
			// 
			this.label4.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label4.Location = new System.Drawing.Point(152, 16);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(112, 16);
			this.label4.TabIndex = 6;
			this.label4.Text = "Nhà cung cấp";
			// 
			// dateTimePicker1
			// 
			this.dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Short;
			this.dateTimePicker1.Location = new System.Drawing.Point(40, 32);
			this.dateTimePicker1.Name = "dateTimePicker1";
			this.dateTimePicker1.Size = new System.Drawing.Size(104, 20);
			this.dateTimePicker1.TabIndex = 3;
			this.dateTimePicker1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.dateTimePicker1_KeyDown);
			// 
			// label2
			// 
			this.label2.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label2.Location = new System.Drawing.Point(16, 16);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(80, 16);
			this.label2.TabIndex = 2;
			this.label2.Text = "Ngày lập";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.btnItemAdd);
			this.groupBox2.Controls.Add(this.lsvView);
			this.groupBox2.Controls.Add(this.btnDelete);
			this.groupBox2.Controls.Add(this.btnUpdate);
			this.groupBox2.Controls.Add(this.btnAdd);
			this.groupBox2.Controls.Add(this.txtAmount);
			this.groupBox2.Controls.Add(this.label10);
			this.groupBox2.Controls.Add(this.txtRate);
			this.groupBox2.Controls.Add(this.label8);
			this.groupBox2.Controls.Add(this.cboUnit);
			this.groupBox2.Controls.Add(this.label7);
			this.groupBox2.Controls.Add(this.txtQty);
			this.groupBox2.Controls.Add(this.label6);
			this.groupBox2.Controls.Add(this.cboItemName);
			this.groupBox2.Controls.Add(this.label5);
			this.groupBox2.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox2.Location = new System.Drawing.Point(8, 64);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(640, 248);
			this.groupBox2.TabIndex = 1;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Thông tin mặt hàng";
			// 
			// btnItemAdd
			// 
			this.btnItemAdd.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnItemAdd.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnItemAdd.Location = new System.Drawing.Point(232, 29);
			this.btnItemAdd.Name = "btnItemAdd";
			this.btnItemAdd.Size = new System.Drawing.Size(32, 23);
			this.btnItemAdd.TabIndex = 17;
			this.btnItemAdd.Text = "...";
			this.btnItemAdd.Click += new System.EventHandler(this.btnItemAdd_Click);
			// 
			// lsvView
			// 
			this.lsvView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																					  this.colItemName,
																					  this.colItemId,
																					  this.colQty,
																					  this.colUnitName,
																					  this.colUnitId,
																					  this.colRate,
																					  this.colAmount});
			this.lsvView.FullRowSelect = true;
			this.lsvView.GridLines = true;
			this.lsvView.HideSelection = false;
			this.lsvView.Location = new System.Drawing.Point(8, 56);
			this.lsvView.MultiSelect = false;
			this.lsvView.Name = "lsvView";
			this.lsvView.Size = new System.Drawing.Size(544, 184);
			this.lsvView.TabIndex = 16;
			this.lsvView.View = System.Windows.Forms.View.Details;
			this.lsvView.SelectedIndexChanged += new System.EventHandler(this.lsvView_SelectedIndexChanged);
			// 
			// colItemName
			// 
			this.colItemName.Text = "Tên mặt hàng";
			this.colItemName.Width = 235;
			// 
			// colItemId
			// 
			this.colItemId.Text = "ItemId";
			this.colItemId.Width = 0;
			// 
			// colQty
			// 
			this.colQty.Text = "Số lượng";
			// 
			// colUnitName
			// 
			this.colUnitName.Text = "Đơn vị";
			// 
			// colUnitId
			// 
			this.colUnitId.Text = "UnitId";
			this.colUnitId.Width = 0;
			// 
			// colRate
			// 
			this.colRate.Text = "Giá (VNĐ)";
			// 
			// colAmount
			// 
			this.colAmount.Text = "Thành tiền";
			this.colAmount.Width = 124;
			// 
			// btnDelete
			// 
			this.btnDelete.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnDelete.Location = new System.Drawing.Point(560, 136);
			this.btnDelete.Name = "btnDelete";
			this.btnDelete.Size = new System.Drawing.Size(72, 24);
			this.btnDelete.TabIndex = 15;
			this.btnDelete.Text = "Xóa";
			this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
			// 
			// btnUpdate
			// 
			this.btnUpdate.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnUpdate.Location = new System.Drawing.Point(560, 104);
			this.btnUpdate.Name = "btnUpdate";
			this.btnUpdate.Size = new System.Drawing.Size(72, 23);
			this.btnUpdate.TabIndex = 14;
			this.btnUpdate.Text = "Sửa";
			this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
			// 
			// btnAdd
			// 
			this.btnAdd.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnAdd.Location = new System.Drawing.Point(560, 72);
			this.btnAdd.Name = "btnAdd";
			this.btnAdd.Size = new System.Drawing.Size(72, 24);
			this.btnAdd.TabIndex = 13;
			this.btnAdd.Text = "Thêm";
			this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
			// 
			// txtAmount
			// 
			this.txtAmount.BackColor = System.Drawing.Color.White;
			this.txtAmount.Location = new System.Drawing.Point(528, 32);
			this.txtAmount.Name = "txtAmount";
			this.txtAmount.ReadOnly = true;
			this.txtAmount.Size = new System.Drawing.Size(96, 20);
			this.txtAmount.TabIndex = 11;
			this.txtAmount.Text = "";
			this.txtAmount.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// label10
			// 
			this.label10.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label10.Location = new System.Drawing.Point(520, 16);
			this.label10.Name = "label10";
			this.label10.Size = new System.Drawing.Size(112, 16);
			this.label10.TabIndex = 10;
			this.label10.Text = "Thành tiền";
			// 
			// txtRate
			// 
			this.txtRate.BackColor = System.Drawing.Color.White;
			this.txtRate.Location = new System.Drawing.Point(440, 32);
			this.txtRate.Name = "txtRate";
			this.txtRate.Size = new System.Drawing.Size(80, 20);
			this.txtRate.TabIndex = 7;
			this.txtRate.Text = "";
			this.txtRate.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			this.txtRate.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtRate_KeyDown);
			this.txtRate.TextChanged += new System.EventHandler(this.txtRate_TextChanged);
			// 
			// label8
			// 
			this.label8.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label8.Location = new System.Drawing.Point(432, 16);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(88, 16);
			this.label8.TabIndex = 6;
			this.label8.Text = "Giá";
			// 
			// cboUnit
			// 
			this.cboUnit.AllowTypeAllSymbols = true;
			this.cboUnit.BackColor = System.Drawing.SystemColors.ControlLightLight;
			this.cboUnit.Location = new System.Drawing.Point(344, 32);
			this.cboUnit.Name = "cboUnit";
			this.cboUnit.Size = new System.Drawing.Size(88, 21);
			this.cboUnit.TabIndex = 5;
			this.cboUnit.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cboUnit_KeyDown);
			this.cboUnit.SelectedIndexChanged += new System.EventHandler(this.cboUnit_SelectedIndexChanged);
			// 
			// label7
			// 
			this.label7.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label7.Location = new System.Drawing.Point(336, 16);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(88, 16);
			this.label7.TabIndex = 4;
			this.label7.Text = "Đơn vị";
			// 
			// txtQty
			// 
			this.txtQty.BackColor = System.Drawing.Color.White;
			this.txtQty.Location = new System.Drawing.Point(272, 32);
			this.txtQty.Name = "txtQty";
			this.txtQty.Size = new System.Drawing.Size(64, 20);
			this.txtQty.TabIndex = 3;
			this.txtQty.Text = "";
			this.txtQty.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			this.txtQty.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtQty_KeyDown);
			this.txtQty.TextChanged += new System.EventHandler(this.txtQty_TextChanged);
			// 
			// label6
			// 
			this.label6.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label6.Location = new System.Drawing.Point(256, 16);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(64, 16);
			this.label6.TabIndex = 2;
			this.label6.Text = "Số lượng";
			// 
			// cboItemName
			// 
			this.cboItemName.AllowTypeAllSymbols = true;
			this.cboItemName.BackColor = System.Drawing.SystemColors.ControlLightLight;
			this.cboItemName.Location = new System.Drawing.Point(80, 32);
			this.cboItemName.Name = "cboItemName";
			this.cboItemName.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.cboItemName.Size = new System.Drawing.Size(144, 21);
			this.cboItemName.TabIndex = 1;
			this.cboItemName.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cboItemName_KeyDown);
			this.cboItemName.SelectedIndexChanged += new System.EventHandler(this.cboItemName_SelectedIndexChanged);
			// 
			// label5
			// 
			this.label5.AutoSize = true;
			this.label5.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label5.Location = new System.Drawing.Point(72, 16);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(74, 16);
			this.label5.TabIndex = 0;
			this.label5.Text = "Tên mặt hàng";
			// 
			// txtTax
			// 
			this.txtTax.BackColor = System.Drawing.Color.Azure;
			this.txtTax.Location = new System.Drawing.Point(128, 32);
			this.txtTax.Name = "txtTax";
			this.txtTax.Size = new System.Drawing.Size(48, 20);
			this.txtTax.TabIndex = 9;
			this.txtTax.Text = "0.0";
			this.txtTax.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			this.txtTax.TextChanged += new System.EventHandler(this.txtTax_TextChanged);
			// 
			// label9
			// 
			this.label9.AutoSize = true;
			this.label9.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label9.Location = new System.Drawing.Point(128, 16);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(48, 16);
			this.label9.TabIndex = 8;
			this.label9.Text = "Thuế(%)";
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.Add(this.txtBillAmount);
			this.groupBox3.Controls.Add(this.label15);
			this.groupBox3.Controls.Add(this.txtDisAmount);
			this.groupBox3.Controls.Add(this.label14);
			this.groupBox3.Controls.Add(this.txtDisPerCent);
			this.groupBox3.Controls.Add(this.label13);
			this.groupBox3.Controls.Add(this.txtTotalTax);
			this.groupBox3.Controls.Add(this.label12);
			this.groupBox3.Controls.Add(this.txtTotalAmount);
			this.groupBox3.Controls.Add(this.label11);
			this.groupBox3.Controls.Add(this.txtTax);
			this.groupBox3.Controls.Add(this.label9);
			this.groupBox3.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox3.Location = new System.Drawing.Point(8, 312);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(640, 64);
			this.groupBox3.TabIndex = 2;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "Tổng số";
			// 
			// txtBillAmount
			// 
			this.txtBillAmount.BackColor = System.Drawing.Color.Azure;
			this.txtBillAmount.Location = new System.Drawing.Point(464, 32);
			this.txtBillAmount.Name = "txtBillAmount";
			this.txtBillAmount.ReadOnly = true;
			this.txtBillAmount.Size = new System.Drawing.Size(160, 20);
			this.txtBillAmount.TabIndex = 9;
			this.txtBillAmount.Text = "0.0";
			this.txtBillAmount.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// label15
			// 
			this.label15.AutoSize = true;
			this.label15.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label15.Location = new System.Drawing.Point(464, 16);
			this.label15.Name = "label15";
			this.label15.Size = new System.Drawing.Size(74, 16);
			this.label15.TabIndex = 8;
			this.label15.Text = "Tổng hóa đơn";
			// 
			// txtDisAmount
			// 
			this.txtDisAmount.BackColor = System.Drawing.Color.Azure;
			this.txtDisAmount.Location = new System.Drawing.Point(352, 32);
			this.txtDisAmount.Name = "txtDisAmount";
			this.txtDisAmount.ReadOnly = true;
			this.txtDisAmount.Size = new System.Drawing.Size(104, 20);
			this.txtDisAmount.TabIndex = 7;
			this.txtDisAmount.Text = "0.0";
			this.txtDisAmount.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// label14
			// 
			this.label14.AutoSize = true;
			this.label14.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label14.Location = new System.Drawing.Point(352, 16);
			this.label14.Name = "label14";
			this.label14.Size = new System.Drawing.Size(72, 16);
			this.label14.TabIndex = 6;
			this.label14.Text = "Tiền giảm giá";
			// 
			// txtDisPerCent
			// 
			this.txtDisPerCent.BackColor = System.Drawing.Color.Azure;
			this.txtDisPerCent.Location = new System.Drawing.Point(280, 32);
			this.txtDisPerCent.Name = "txtDisPerCent";
			this.txtDisPerCent.Size = new System.Drawing.Size(64, 20);
			this.txtDisPerCent.TabIndex = 5;
			this.txtDisPerCent.Text = "0.0";
			this.txtDisPerCent.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			this.txtDisPerCent.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtDisPerCent_KeyDown);
			this.txtDisPerCent.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtDisPerCent_KeyPress);
			this.txtDisPerCent.TextChanged += new System.EventHandler(this.txtDisPerCent_TextChanged);
			// 
			// label13
			// 
			this.label13.AutoSize = true;
			this.label13.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label13.Location = new System.Drawing.Point(280, 16);
			this.label13.Name = "label13";
			this.label13.Size = new System.Drawing.Size(67, 16);
			this.label13.TabIndex = 4;
			this.label13.Text = "Giảm giá(%)";
			// 
			// txtTotalTax
			// 
			this.txtTotalTax.BackColor = System.Drawing.Color.Azure;
			this.txtTotalTax.Location = new System.Drawing.Point(184, 32);
			this.txtTotalTax.Name = "txtTotalTax";
			this.txtTotalTax.ReadOnly = true;
			this.txtTotalTax.Size = new System.Drawing.Size(88, 20);
			this.txtTotalTax.TabIndex = 3;
			this.txtTotalTax.Text = "0.0";
			this.txtTotalTax.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// label12
			// 
			this.label12.AutoSize = true;
			this.label12.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label12.Location = new System.Drawing.Point(184, 16);
			this.label12.Name = "label12";
			this.label12.Size = new System.Drawing.Size(51, 16);
			this.label12.TabIndex = 2;
			this.label12.Text = "Tiền thuế";
			// 
			// txtTotalAmount
			// 
			this.txtTotalAmount.BackColor = System.Drawing.Color.Azure;
			this.txtTotalAmount.Location = new System.Drawing.Point(16, 32);
			this.txtTotalAmount.Name = "txtTotalAmount";
			this.txtTotalAmount.ReadOnly = true;
			this.txtTotalAmount.Size = new System.Drawing.Size(104, 20);
			this.txtTotalAmount.TabIndex = 1;
			this.txtTotalAmount.Text = "0.0";
			this.txtTotalAmount.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// label11
			// 
			this.label11.AutoSize = true;
			this.label11.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label11.Location = new System.Drawing.Point(16, 16);
			this.label11.Name = "label11";
			this.label11.Size = new System.Drawing.Size(51, 16);
			this.label11.TabIndex = 0;
			this.label11.Text = "Tổng tiền";
			// 
			// btnSave
			// 
			this.btnSave.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnSave.Location = new System.Drawing.Point(480, 384);
			this.btnSave.Name = "btnSave";
			this.btnSave.Size = new System.Drawing.Size(80, 24);
			this.btnSave.TabIndex = 3;
			this.btnSave.Text = "Lập hóa đơn";
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			// 
			// btnClose
			// 
			this.btnClose.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnClose.Location = new System.Drawing.Point(568, 384);
			this.btnClose.Name = "btnClose";
			this.btnClose.Size = new System.Drawing.Size(80, 24);
			this.btnClose.TabIndex = 4;
			this.btnClose.Text = "Thoát";
			this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
			// 
			// frmEditPurchaseBill
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(656, 412);
			this.Controls.Add(this.btnClose);
			this.Controls.Add(this.btnSave);
			this.Controls.Add(this.groupBox3);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "frmEditPurchaseBill";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Chỉnh sửa hóa đơn nhập hàng";
			this.TopMost = true;
			this.Load += new System.EventHandler(this.frmPOSPurchaseBill_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void btnClose_Click(object sender, System.EventArgs e)
		{
			//cap nhat lai so luong hang hoa neu nhu khong chinh sua hoa don
			for(int i = 0; i < danhSachMatHang.Rows.Count; i++)
			{
				int itemId = Convert.ToInt32(this.danhSachMatHang.Rows[i]["itemId"].ToString());
				double soLuongTheoDonViDuocChon = Convert.ToDouble(this.danhSachMatHang.Rows[i]["qty"].ToString());
				int unitId = Convert.ToInt32(this.danhSachMatHang.Rows[i]["unitId"].ToString());
				int donViChuanId = clsItem.GetDonViChuanId(itemId);
				double factor = clsUnitConversion.GetFactor(itemId, donViChuanId, unitId);
				double soLuongTheoDonViChuan = soLuongTheoDonViDuocChon / factor;
				clsStock.UpdateQuantity(itemId, clsGlobal.g_DepartmentId, soLuongTheoDonViChuan, "+");
			}	
			this.Close();
		}

		public bool IsDecimal(string theValue)
		{
			try
			{
				Convert.ToDouble(theValue);
				return true;
			} 
			catch 
			{
				return false;
			}
		}

		private void btnAdd_Click(object sender, System.EventArgs e)
		{
			bool isLegal = true;
			string strAlertMessage = "";
			if(!this.IsDecimal(this.txtQty.Text))
			{
				isLegal = false;
				strAlertMessage += "Số lượng phải là một số thực khác 0. ";
			}
			if(!this.IsDecimal(this.txtRate.Text))
			{
				isLegal = false;
				strAlertMessage += "Giá phải là một số thực";
			}
			if(this.cboItemName.SelectedItem == null)
			{
				isLegal = false;
				strAlertMessage += "Hãy chọn một mặt hàng để thêm. ";
			}
			else
			{
				for(int i = 0; i < this.lsvView.Items.Count; i++)
				{
					if(this.lsvView.Items[i].SubItems[1].Text == this.cboItemName.SelectedValue.ToString())
					{
						isLegal = false;
						strAlertMessage += "Mặt hàng này đã có trong danh sách. ";
						break;
					}
				}
			}

			if(this.cboUnit.SelectedItem == null)
			{
				isLegal = false;
				strAlertMessage += "Hãy chọn một đơn vị. ";
			}
			
			//Them muc hang vao lsvView
			if(isLegal)
			{
				ListViewItem lvi = new ListViewItem(new string[]{this.cboItemName.Text, 
																	this.cboItemName.SelectedValue.ToString(), 
																	this.txtQty.Text, 
																	this.cboUnit.Text, 
																	this.cboUnit.SelectedValue.ToString(), 
																	this.txtRate.Text, 
																	this.txtAmount.Text});
				this.lsvView.Items.Add(lvi);
				this.TinhToanTongGiaTri();
				this.cboItemName.Focus();
			}
			else
			{
				MessageBox.Show(strAlertMessage);
			}
		}

		private void TinhToanTongGiaTri()
		{
			//tinh toan cac gia tri hien thi o cac textbox ben duoi
			double dbTotalAmount = 0.0;
			for(int i = 0; i < this.lsvView.Items.Count; i++)
			{
				dbTotalAmount += Convert.ToDouble(this.lsvView.Items[i].SubItems[6].Text);
			}
			this.txtTotalAmount.Text = dbTotalAmount.ToString();

			double dbTaxAmount = 0.0;
			if(!this.IsDecimal(this.txtTax.Text))
			{
				MessageBox.Show("Tỷ lệ thuế nhập vào phải là một số thực");
			}
			else
			{
				dbTaxAmount = dbTotalAmount*Convert.ToDouble(this.txtTax.Text);
			}
			this.txtTotalTax.Text = dbTaxAmount.ToString();
			
			double dbDiscountAmount = 0.0;
			if(!this.IsDecimal(this.txtDisPerCent.Text))
			{
				MessageBox.Show("Tỷ lệ giảm giá phải là một số thực. ");
			}
			else
			{
				dbDiscountAmount = dbTotalAmount*Convert.ToDouble(this.txtDisPerCent.Text);
			}
			this.txtDisAmount.Text = dbDiscountAmount.ToString();

			double dbBillAmount = dbTotalAmount - dbTaxAmount - dbDiscountAmount;
			this.txtBillAmount.Text = dbBillAmount.ToString();
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			if(this.lsvView.Items.Count == 0)
			{
				MessageBox.Show("Không có mục hàng nào trong hóa đơn");
			}
			else
			{
				//cap nhat bang tblPurchase
				clsPurchase pc = new clsPurchase();
				pc.BillDate = this.dateTimePicker1.Text;
				pc.Total = Convert.ToDouble(this.txtTotalAmount.Text);
				pc.Tax = Convert.ToDouble(this.txtTax.Text);
				pc.Discount = Convert.ToDouble(this.txtDisPerCent.Text);
				pc.BillAmount = Convert.ToDouble(this.txtBillAmount.Text);
				if(this.cboVendor.SelectedItem == null)
				{
					pc.SupplierId = -1;
				}
				else
				{
					pc.SupplierId = Convert.ToInt32(this.cboVendor.SelectedValue);
				}
				pc.ChinhSuaHoaDonNhap(this.purchaseId);

				//cap nhat bang tblPurchaseDetail
				//xoa thong tin chi tiet ve hoa don cu
				clsPurchaseDetail.XoaHoaDon(this.purchaseId);

				//cap nhat thong tin hoa don moi vao bang tblpurchasedetail
				for(int i = 0; i < this.lsvView.Items.Count; i++)
				{
					clsPurchaseDetail pd = new clsPurchaseDetail();
					pd.PurchaseId = this.purchaseId;
					pd.ItemId = Convert.ToInt32(this.lsvView.Items[i].SubItems[1].Text);
					pd.Quantity = Convert.ToDouble(this.lsvView.Items[i].SubItems[2].Text);
					pd.UnitId = Convert.ToInt32(this.lsvView.Items[i].SubItems[4].Text);
					pd.Rate = Convert.ToDouble(this.lsvView.Items[i].SubItems[5].Text);
					pd.Amount = Convert.ToDouble(this.lsvView.Items[i].SubItems[6].Text);
					pd.Add();
				}

				//cap nhat lai so luong hang hoa vao kho
				for(int j = 0; j < this.lsvView.Items.Count; j++)
				{
					int intItemId = Convert.ToInt32(this.lsvView.Items[j].SubItems[1].Text);
					int donViChuanId = clsItem.GetDonViChuanId(intItemId);
					int intUnitId = Convert.ToInt32(this.lsvView.Items[j].SubItems[4].Text);
					double dbFactor = clsUnitConversion.GetFactor(intItemId, donViChuanId, intUnitId);
					double dbQuantity = Convert.ToDouble(this.lsvView.Items[j].SubItems[2].Text) / dbFactor;
					clsStock.UpdateQuantity(intItemId, clsGlobal.g_DepartmentId, dbQuantity, "+");
				}

				//thong bao va dong form
				MessageBox.Show("Hóa đơn đã được cập nhật thành công. ");
				this.Close();
			}
		}

		private void cboItemName_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if(this.cboItemName.DisplayMember == "itemName" && this.cboItemName.ValueMember == "itemId")
			{
				int intItemId = Convert.ToInt32(cboItemName.SelectedValue.ToString());
				cboUnit.DataSource = clsUnitConversion.GetUnitForAnItem(intItemId);
				cboUnit.DisplayMember = "unitName";
				cboUnit.ValueMember = "unitId";
			}
		}

		private void btnVendorSelect_Click(object sender, System.EventArgs e)
		{
			clsGlobal.g_isAddVendorFromPurchase = true;
			frmAddEditVendor _frmAddEditVendor = new frmAddEditVendor();
			_frmAddEditVendor.TypeUse=1;
			_frmAddEditVendor.ShowDialog();

			DataTable dt = clsVendor.GetAll();
			cboVendor.DataSource = dt;
			cboVendor.DisplayMember = "SupplierName";
			cboVendor.ValueMember = "SupplierId";
			cboVendor.Text = clsGlobal.g_VendorName;
		}

		#region Create delegate.
		//Khai bao con tro ham.
		public delegate  void Call(String userName);

		//Ham ma no thuc su tro den.
		public static void CallMethod(String userName)
		{
			DataTable dt = clsVendor.GetAll();
			//			cboVendor.DataSource = dt;
			//			cboVendor.DisplayMember = "SupplierName";
			//			cboVendor.ValueMember = "SupplierId";
			//			cboVendor.Text = clsGlobal.g_VendorName;
		}
		#endregion
		
		private void frmPOSPurchaseBill_Load(object sender, System.EventArgs e)
		{
			DataTable dtVendor = clsVendor.GetAll();
			cboVendor.DataSource = dtVendor;
			cboVendor.DisplayMember = "SupplierName";
			cboVendor.ValueMember = "SupplierId";

			DataTable dt = clsStock.GetAllItemForAnDepartment(clsGlobal.g_DepartmentId);
			this.cboItemName.DataSource = dt;
			this.cboItemName.DisplayMember = "itemName";
			this.cboItemName.ValueMember = "itemId";

			DataTable danhSachDonVi = clsUnit.GetAll();
			this.cboUnit.DataSource = danhSachDonVi;
			this.cboUnit.DisplayMember = "unitName";
			this.cboUnit.ValueMember = "unitId";

			//Load du lieu ve hoa don tu bang tblPurchase len form
			DataTable hoaDonNhap = clsPurchase.getPurchaseBillInformation(this.purchaseId);
			this.dateTimePicker1.Text = hoaDonNhap.Rows[0]["billDate"].ToString();
			if(Convert.ToInt32(hoaDonNhap.Rows[0]["supplierId"]) != -1)
			{
				string supplierName = clsVendor.GetSupplierName(Convert.ToInt32(hoaDonNhap.Rows[0]["supplierId"]));
				int i = this.cboVendor.FindString(supplierName);
				this.cboVendor.SelectedItem = this.cboVendor.Items[i];
			}
			this.txtTotalAmount.Text = hoaDonNhap.Rows[0]["total"].ToString();
			this.txtTax.Text = hoaDonNhap.Rows[0]["tax"].ToString();
			this.txtDisPerCent.Text = hoaDonNhap.Rows[0]["discount"].ToString();
			this.txtBillAmount.Text = hoaDonNhap.Rows[0]["billAmount"].ToString();

			double luongTienThue = Convert.ToDouble(this.txtTotalAmount.Text) * Convert.ToDouble(this.txtTax.Text);
			this.txtTotalTax.Text = luongTienThue.ToString();

			double luongTienGiamGia = Convert.ToDouble(this.txtTotalAmount.Text) * Convert.ToDouble(this.txtDisPerCent.Text);
			this.txtDisAmount.Text = luongTienGiamGia.ToString();

			//Load du lieu tu tblPurchaseDetail len listview
			//Cap nhat lai so luong hang hoa trong kho nhu chua tung co hoa don nay duoc cap nhat
			this.danhSachMatHang = clsPurchaseDetail.GetDanhSachMatHang(this.purchaseId);
			for(int i = 0; i < danhSachMatHang.Rows.Count; i++)
			{
				ListViewItem lvi = new ListViewItem(new string[]{danhSachMatHang.Rows[i]["itemName"].ToString(), 
					danhSachMatHang.Rows[i]["itemId"].ToString(), danhSachMatHang.Rows[i]["qty"].ToString(), 
					danhSachMatHang.Rows[i]["unitName"].ToString(), danhSachMatHang.Rows[i]["unitId"].ToString(), 
					danhSachMatHang.Rows[i]["rate"].ToString(), danhSachMatHang.Rows[i]["amount"].ToString()});
				this.lsvView.Items.Add(lvi);

				int itemId = Convert.ToInt32(this.danhSachMatHang.Rows[i]["itemId"].ToString());
				double soLuongTheoDonViDuocChon = Convert.ToDouble(this.danhSachMatHang.Rows[i]["qty"].ToString());
				int unitId = Convert.ToInt32(this.danhSachMatHang.Rows[i]["unitId"].ToString());
				int donViChuanId = clsItem.GetDonViChuanId(itemId);
				double factor = clsUnitConversion.GetFactor(itemId, donViChuanId, unitId);
				double soLuongTheoDonViChuan = soLuongTheoDonViDuocChon / factor;
				clsStock.UpdateQuantity(itemId, clsGlobal.g_DepartmentId, soLuongTheoDonViChuan, "-");
			}
		}

		private void txtQty_TextChanged(object sender, System.EventArgs e)
		{
			if(!this.IsDecimal(this.txtQty.Text))
			{
				MessageBox.Show("Số lượng phải là một số thực khác 0. ");
			}
			else
			{
				if(this.IsDecimal(this.txtRate.Text))
				{
					double dbQty = Convert.ToDouble(this.txtQty.Text);
					double dbAmount = dbQty*Convert.ToDouble(this.txtRate.Text);
					this.txtAmount.Text = dbAmount.ToString();
				}
			}
		}

		private void txtRate_TextChanged(object sender, System.EventArgs e)
		{
			if(!this.IsDecimal(this.txtRate.Text))
			{
				MessageBox.Show("Giá thành nhập vào phải là số thực. ");
			}
			else
			{
				if(this.IsDecimal(this.txtQty.Text))
				{
					double dbRate = Convert.ToDouble(this.txtRate.Text);
					double dbAmount = dbRate*Convert.ToDouble(this.txtQty.Text);
					this.txtAmount.Text = dbAmount.ToString();
				}
			}
		}
	
		private void txtDisPerCent_TextChanged(object sender, System.EventArgs e)
		{
			if(!this.IsDecimal(this.txtDisPerCent.Text))
			{
				MessageBox.Show("Tỷ lệ giảm giá phải là một số thực. ");
			}
			else
			{
				double dbDisPercent = Convert.ToDouble(this.txtDisPerCent.Text);
				double dbTotalAmount = Convert.ToDouble(this.txtTotalAmount.Text);
				double dbDiscountAmount = dbDisPercent*dbTotalAmount;
				this.txtDisAmount.Text = dbDiscountAmount.ToString();

				double dbTaxAmount = Convert.ToDouble(this.txtTotalTax.Text);
				double dbBillAmount = dbTotalAmount - dbDiscountAmount - dbTaxAmount;
				this.txtBillAmount.Text = dbBillAmount.ToString();
			}
		}

		private void txtTax_TextChanged(object sender, System.EventArgs e)
		{
			if(!this.IsDecimal(this.txtTax.Text))
			{
				MessageBox.Show("Tỷ lệ thuế phải là số thực. ");
			}
			else
			{
				double dbTaxPercent = Convert.ToDouble(this.txtTax.Text);
				double dbTotalAmount = Convert.ToDouble(this.txtTotalAmount.Text);
				double dbTaxAmount = dbTaxPercent*dbTotalAmount;
				this.txtTotalTax.Text = dbTaxAmount.ToString();

				double dbDiscountAmount = Convert.ToDouble(this.txtDisAmount.Text);
				double dbBillAmount = dbTotalAmount - dbTaxAmount - dbDiscountAmount;
				this.txtBillAmount.Text = dbBillAmount.ToString();
			}
		}
		
		private void txtQty_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				this.cboUnit.Focus();
			}
		}

		private void txtRate_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				this.btnAdd.Focus();
			}
		}

		private void btnUpdate_Click(object sender, System.EventArgs e)
		{
			if(this.lsvView.FocusedItem == null)
			{
				MessageBox.Show("Hãy chọn một danh mục hàng để chỉnh sửa. ");
			}
			else
			{
				bool isLegal = true;
				string strAlertMessage = "";
				if(!this.IsDecimal(this.txtQty.Text))
				{
					isLegal = false;
					strAlertMessage += "Số lượng phải là một số thực khác 0. ";
				}
				if(!this.IsDecimal(this.txtRate.Text))
				{
					isLegal = false;
					strAlertMessage += "Giá phải là một số thực";
				}
				if(this.cboItemName.SelectedItem == null)
				{
					isLegal = false;
					strAlertMessage += "Hãy chọn một mặt hàng để thêm. ";
				}
				else
				{
					int intSelectedIndex = this.lsvView.FocusedItem.Index;
					for(int i = 0; i < this.lsvView.Items.Count; i++)
					{
						if(i != intSelectedIndex && this.cboItemName.SelectedValue.ToString() == this.lsvView.Items[i].SubItems[1].Text)
						{
							isLegal = false;
							strAlertMessage += "Mặt hàng trùng với một mặt hàng khác đã có trong danh sách. ";
							break;
						}
					}
				}

				if(this.cboUnit.SelectedItem == null)
				{
					isLegal = false;
					strAlertMessage += "Hãy chọn một đơn vị. ";
				}
				if(!isLegal)
				{
					MessageBox.Show(strAlertMessage);
				}
				else
				{
					this.lsvView.FocusedItem.SubItems[0].Text = this.cboItemName.Text;
					this.lsvView.FocusedItem.SubItems[1].Text = this.cboItemName.SelectedValue.ToString();
					this.lsvView.FocusedItem.SubItems[2].Text = this.txtQty.Text;
					this.lsvView.FocusedItem.SubItems[3].Text = this.cboUnit.Text;
					this.lsvView.FocusedItem.SubItems[4].Text = this.cboUnit.SelectedValue.ToString();
					this.lsvView.FocusedItem.SubItems[5].Text = this.txtRate.Text;
					this.lsvView.FocusedItem.SubItems[6].Text = this.txtAmount.Text;

					this.TinhToanTongGiaTri();
				}
			}
		}

		private void btnDelete_Click(object sender, System.EventArgs e)
		{
			if(this.lsvView.FocusedItem == null)
			{
				MessageBox.Show("Hãy chọn một mặt hàng để xóa. ");
			}
			else
			{
				int intFocusedIndex = this.lsvView.FocusedItem.Index;
				this.lsvView.FocusedItem.Remove();
				if(this.lsvView.Items.Count > intFocusedIndex)
				{
					this.lsvView.Items[intFocusedIndex].Selected = true;
				}
				else
				{
					if(this.lsvView.Items.Count > 0)
					{
						this.lsvView.Items[intFocusedIndex - 1].Selected = true;
					}
				}

				this.TinhToanTongGiaTri();
			}
		}		
		
		//ham nay cua LVD chuyen du lieu tw table sang list view (c) LVD TIN4 K47
		private void Table2LView(DataTable dt, ref ListView lv)
		{
			lv.Items.Clear();

			//tao cac hang
			for (int i=0;i<dt.Rows.Count;i++)
			{				
				ListViewItem item1 =new ListViewItem(dt.Rows[i][0].ToString());
				item1.SubItems.Add(dt.Rows[i][1].ToString());
				item1.SubItems.Add(dt.Rows[i][2].ToString());
				item1.SubItems.Add(dt.Rows[i][3].ToString());
				item1.SubItems.Add(dt.Rows[i][5].ToString());
				lv.Items.Add(item1);
			}
		}

		private void txtDisPerCent_KeyDown(object sender, KeyEventArgs e)
		{
		}
		private void txtDisPerCent_KeyPress(object sender, KeyPressEventArgs e)
		{
		}

		private void btnItemAdd_Click(object sender, System.EventArgs e)
		{
			//Them vao bang tblItem
			frmItemDefinition _frmItemDefinition = new frmItemDefinition();
			_frmItemDefinition.Show();

			//Them vao bang tblItemDepartment

			//Cho cai vua load lai len dau.
		}

		private void cboVendor_SelectedIndexChanged(object sender, System.EventArgs e)
		{
		
		}

		private void txtVoucher_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				this.dateTimePicker1.Focus();
			}
		}

		private void dateTimePicker1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				this.cboVendor.Focus();
			}
			else
			{
				ComboBox combo=(ComboBox)sender;
				if(!combo.DroppedDown) combo.DroppedDown = true;
			}
		}

		private void cboVendor_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				this.cboItemName.Focus();
			}
			else
			{
				ComboBox combo=(ComboBox)sender;
				if(!combo.DroppedDown) combo.DroppedDown = true;
			}
		}

		private void cboItemName_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				this.txtQty.Focus();
			}
			else
			{
				ComboBox combo=(ComboBox)sender;
				if(!combo.DroppedDown) combo.DroppedDown = true;
			}
		}

		private void cboUnit_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				this.txtRate.Focus();
			}
			else
			{
				ComboBox combo=(ComboBox)sender;
				if(!combo.DroppedDown) combo.DroppedDown = true;
			}
		}

		private void cboUnit_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if(this.cboItemName.SelectedItem != null && this.cboUnit.DisplayMember == "unitName" && this.cboUnit.ValueMember == "unitId")
			{
				int intItemId = Convert.ToInt32(this.cboItemName.SelectedValue);
				int intUnitId = Convert.ToInt32(this.cboUnit.SelectedValue);
				this.txtRate.Text = clsRate.GetRate(intItemId, intUnitId).ToString();
			}
		}

		private void lsvView_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if(this.lsvView.FocusedItem != null)
			{
				int intIndex = this.cboItemName.FindString(this.lsvView.FocusedItem.SubItems[0].Text);
				if(intIndex == -1)
				{
					MessageBox.Show("Mặt hàng này đã bị xóa khỏi bảng tblItem. ");	
				}
				else
				{
					this.cboItemName.SelectedItem = this.cboItemName.Items[intIndex];
				}

				this.txtQty.Text = this.lsvView.FocusedItem.SubItems[2].Text;

				intIndex = this.cboUnit.FindString(this.lsvView.FocusedItem.SubItems[3].Text);
				this.cboUnit.SelectedItem = this.cboUnit.Items[intIndex];

				this.txtRate.Text = this.lsvView.FocusedItem.SubItems[5].Text;

				this.txtAmount.Text = this.lsvView.FocusedItem.SubItems[6].Text;
			}
		}		
	}
}
